<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="客户姓名" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户姓名"></el-input>
    </el-form-item>
    <el-form-item label="客户手机号" prop="customerPhone">
      <el-input v-model="dataForm.customerPhone" placeholder="客户手机号"></el-input>
    </el-form-item>
    <el-form-item label="客户地址" prop="customerAdress">
      <el-input v-model="dataForm.customerAdress" placeholder="客户地址"></el-input>
    </el-form-item>
      <el-form-item label="客户邮箱" prop="customerEmile">
        <el-input v-model="dataForm.customerEmile" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="客户类型" prop="customerType">
        <el-select v-model="dataForm.customerType" clearable >
          <el-option value="1" label="租赁客户" key="1"></el-option>
          <el-option value="2" label="配送客户" key="2"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          id: 0,
          entId: '',
          customerName: '',
          customerPhone: '',
          customerEmile: '',
          customerType: '',
          customerAdress: '',
          delFlag: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: ''
        },
        dataRule: {
          customerName: [
            { required: true, message: '客户姓名不能为空', trigger: 'blur' }
          ],
          customerPhone: [
            { required: true, message: '客户手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          customerEmile: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' }
          ],
          customerType: [
            { required: true, message: '客户类型不能为空', trigger: 'blur' }
          ],
          customerAdress: [
            { required: true, message: '客户地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcustomer/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.entId = data.entCustomer.entId
                this.dataForm.customerName = data.entCustomer.customerName
                this.dataForm.customerPhone = data.entCustomer.customerPhone
                this.dataForm.customerEmile = data.entCustomer.customerEmile
                this.dataForm.customerType = data.entCustomer.customerType
                this.dataForm.customerAdress = data.entCustomer.customerAdress
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcustomer/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'entId': this.dataForm.entId,
                'customerName': this.dataForm.customerName,
                'customerPhone': this.dataForm.customerPhone,
                'customerEmile': this.dataForm.customerEmile,
                'customerType': this.dataForm.customerType,
                'customerAdress': this.dataForm.customerAdress
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
